package com.xiehua.task.intf;


import com.xiehua.task.msg.BaseBody;
import com.xiehua.task.protocol.TaskPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.utils.json.Json;

/**
 * 业务处理器接口
 * @author Mr.zhang
 * 2018年3月27日 下午9:52:42
 */
public abstract class AbsBusinessHandler <T extends BaseBody> implements BusinessHandlerIntf{

    private static Logger log = LoggerFactory.getLogger(AbsBusinessHandler.class);

    /**
     *
     * @author Mr.zhang
     */
    public AbsBusinessHandler() {
    }

    public abstract Class<T> bodyClass();

    @Override
    public Object handler(TaskPacket packet, ChannelContext channelContext) throws Exception {
        String jsonStr = null;
        T bsBody = null;
        if (packet.getBody() != null) {
            jsonStr = new String(packet.getBody(), TaskPacket.CHARSET);
            bsBody = Json.toBean(jsonStr, bodyClass());
        }

        return handler(packet, bsBody, channelContext);
    }

    public abstract Object handler(TaskPacket packet, T bsBody, ChannelContext channelContext) throws Exception;



}
